﻿using CSharpDay11__Entity_Framework_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CSharpDay11__Entity_Framework_.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<ArtistViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Artists.Where(x => x.DateDeleted == null).Select(x => new ArtistViewModel()
                {
                    Id = x.Id,
                    DateDeleted = null,
                    ImageUrl = x.ImageUrl,
                    IsAlive = x.IsAlive,
                    Name = x.Name,
                    Artworks = x.Artworks.Where(a => a.DateDeleted == null).ToList()
                }).ToList();
            }
            return View(model);
        }

        public ActionResult AddArtist()
        {
            return View(new Artist());
        }

        [HttpPost]
        public ActionResult AddArtist(Artist model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    db.Artists.Add(model);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult ArtistDetails(int? id)
        {
            if (id == null) return RedirectToAction("Index");
            Artist model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Artists.Include("Artworks").FirstOrDefault(x => x.Id == id);
            }
            if (model == null) return RedirectToAction("Index");
            return View(model);
        }

        public ActionResult CreateArtwork(int? id)
        {
            if (id == null) return RedirectToAction("Index");
            Artwork model = new Artwork();
            model.ArtistId = id.Value;
            return View(model);
        }

        [HttpPost]
        public ActionResult CreateArtwork(Artwork model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    db.ArtWorks.Add(model);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult DeleteArtist(int? id)
        {
            if (id != null)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Artist model = db.Artists.FirstOrDefault(x => x.Id == id);
                    model.DateDeleted = DateTime.Now;
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult ArtworkDetails(int? id)
        {
            if (id == null) return RedirectToAction("Index");
            Artwork model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.ArtWorks.Include("Artist").FirstOrDefault(x => x.Id == id);
            }
            if (model == null || model.DateDeleted != null) return RedirectToAction("Index");
            return View(model);
        }

        public ActionResult DeleteArtwork(int? id)
        {
            if (id != null)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Artwork model = db.ArtWorks.FirstOrDefault(x => x.Id == id);
                    model.DateDeleted = DateTime.Now;
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult EditArtist(int? id)
        {
            if (id == null) return RedirectToAction("Index");
            Artist model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Artists.FirstOrDefault(x => x.Id == id);
            }
            return View("AddArtist", model);
        }
        [HttpPost]
        public ActionResult EditArtist(Artist model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Artist dbArtist = db.Artists.FirstOrDefault(x => x.Id == model.Id);
                    if (dbArtist != null)
                    {
                        dbArtist.Name = model.Name;
                        dbArtist.IsAlive = model.IsAlive;
                        dbArtist.ImageUrl = model.ImageUrl;
                        db.SaveChanges();
                    }
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult EditArtwork(int? id)
        {
            if (id == null) return RedirectToAction("Index");
            Artwork model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.ArtWorks.FirstOrDefault(x => x.Id == id);
            }
            if (model == null) return RedirectToAction("Index");
            return View("CreateArtwork", model);
        }

        [HttpPost]
        public ActionResult EditArtwork(Artwork model)
        {
            if (model.Id != null && ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Artwork dbArtwork = db.ArtWorks.FirstOrDefault(x => x.Id == model.Id);
                    if (dbArtwork != null)
                    {
                        dbArtwork.ImageUrl = model.ImageUrl;
                        dbArtwork.Title = model.Title;
                        dbArtwork.Worth = model.Worth;
                        dbArtwork.AwesomenessScale = model.AwesomenessScale;
                        db.SaveChanges();
                    }
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}